package GraphsServlets;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;

/**
 * Servlet implementation class GraphServletTest
 */
public class GraphServletTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GraphServletTest() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		firstChart(500, 500).get;
		//byte[] imageBytes = getImageAsBytes();
		System.out.println("I am here");
		try{
			response.setContentType("image/png");
			//response.setContentLength(imageBytes.length);
			OutputStream outputStream = response.getOutputStream();
			ImageIO.write(firstChart("My products status", "Price", "Days",500, 500), "png", response.getOutputStream());
			outputStream.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		//response.getOutputStream().write(imageBytes);
	}
		
	 //Defining a very simple chart.
	private BufferedImage firstChart(String title, String xAxis, String yAxis, int width, int height) throws Exception{
		//Source: The JFreeChart Class Library, Reference documentations
//		XYSeries series1 = new XYSeries("First");
		//series1.add(1.0, new Date()); 
//		series1.add(2.0, 4.0); 
		XYSeries series1 = new XYSeries("First Product");
		series1.add(1.0, 1.0); 
		series1.add(2.0, 4.0); 
		series1.add(3.0, 3.0); 
		series1.add(4.0, 5.0); 
		series1.add(5.0, 5.0); 
		series1.add(6.0, 7.0); 
		series1.add(7.0, 7.0); 
		series1.add(8.0, 30.0);
		
		XYSeries series2 = new XYSeries("Second Product"); 
		series2.add(1.0, 5.0);
		series2.add(2.0, 7.0);
		series2.add(3.0, 6.0);
		series2.add(4.0, 8.0); 
		series2.add(5.0, 4.0); 
		series2.add(6.0, 4.0); 
		series2.add(7.0, 2.0); 
		series2.add(8.0, 30.0);
		
		XYSeries series3 = new XYSeries("Third Product"); 
		series3.add(3.0, 4.0);
		series3.add(4.0, 3.0);
		series3.add(5.0, 2.0);
		series3.add(6.0, 3.0); 
		series3.add(7.0, 6.0); 
		series3.add(8.0, 3.0); 
		series3.add(9.0, 4.0); 
		series3.add(10.0, 30.0);
		
		XYSeriesCollection dataset = new XYSeriesCollection(); 
		dataset.addSeries(series1); 
		dataset.addSeries(series2); 
		dataset.addSeries(series3);
		
		JFreeChart chart = ChartFactory.createXYLineChart(title, xAxis, yAxis, dataset, PlotOrientation.HORIZONTAL, true, true, true);
		return chart.createBufferedImage(width, height);
	}
}
